const express = require('express');
const userRoutes = require('./userRoutes');
const couponRoutes = require('./couponRoutes');
const adminRoutes = require('./adminRoutes');
const roomRoutes = require('./roomRoutes');
const reservationRoutes = require('./reservationRoutes');
const orderRoutes = require('./orderRoutes');
const payRoutes = require('./payRoutes');
const paymentRoutes = require('./paymentRoutes');
const testRoutes = require('./test');

const router = express.Router();

// 输出路由调试信息
console.log('====== 注册路由模块 ======');

// 测试专用API - 确保服务器运行正常
router.get('/api/healthcheck', (req, res) => {
  res.status(200).json({
    code: 200,
    message: '服务器运行正常',
    time: new Date().toISOString()
  });
});
console.log('已注册: /api/healthcheck');

// 移动端支付API路由 - 优先注册支付路由，避免其他中间件拦截
router.use('/api/mobile/pay', payRoutes);
console.log('已注册: /api/mobile/pay (优先处理)');

// 移动端用户API路由
router.use('/api/mobile/user', userRoutes);
console.log('已注册: /api/mobile/user');

// 移动端优惠券API路由
router.use('/api/mobile/coupon', couponRoutes);
console.log('已注册: /api/mobile/coupon');

// 移动端房间API路由
router.use('/api/mobile/room', roomRoutes);
console.log('已注册: /api/mobile/room');

// 移动端预订API路由
router.use('/api/mobile/reservation', reservationRoutes);
console.log('已注册: /api/mobile/reservation');

// 移动端订单API路由
router.use('/api/mobile/order', orderRoutes);
console.log('已注册: /api/mobile/order');

// 移动端支付API路由 - 备用路由
router.use('/api/mobile/payment', paymentRoutes);
console.log('已注册: /api/mobile/payment (备用路由)');

// 管理端API路由
router.use('/api/admin', adminRoutes);
console.log('已注册: /api/admin');

// 测试路由
router.use('/api/test', testRoutes);
console.log('已注册: /api/test');

console.log('========================');

// 处理404路由 - 确保在所有有效路由注册后再处理404
router.use('*', (req, res) => {
  // 添加调试日志，帮助排查路由问题
  console.log(`[404] 未找到路由: ${req.method} ${req.originalUrl}`);
  
  res.status(404).json({
    code: 404,
    message: '请求的资源不存在',
    data: null
  });
});

module.exports = router; 